package com.baiyinet.util.webview;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;

import com.baiyinet.R;
import com.baiyinet.ui.ToolbarOwner;

public class CustomWebChromeClient extends WebChromeClient {
    private ToolbarOwner.IToolbar mToolbar;

    public CustomWebChromeClient(ToolbarOwner.IToolbar toolbar) {
        mToolbar = toolbar;
    }

    /**
     * 根据页面的title来选择显示titleBar的图片内容
     */
    @Override
    public void onReceivedTitle(WebView view, String title) {
        if (TextUtils.isEmpty(mToolbar.getToolbarTitle())) {
            mToolbar.setToolbarTitle(title);
        }

//        int resId = 0;
//
//        // 不用switch语句的原因: JDK6不支持字符串,且不支持Enum中文
//        if (title.equals("登录")) {
//            resId = R.drawable.pic_title_login;
//        } else if (title.equals("新增车牌")) {
//            resId = R.drawable.pic_title_add_car;
//        } else if (title.equals("个人中心")) {
//            resId = R.drawable.img_title_main;
//        } else if (title.equals("进入地图")) {
//            resId = R.drawable.pic_title_find_car_parks;
//        } else if (title.equals("车牌管理")) {
//            resId = R.drawable.pic_title_car_management;
//        } else if (title.equals("车辆验证")) {
//            resId = R.drawable.pic_title_my_car;
//        } else if (title.equals("送券商家") || title.equals("我的优惠")) {
//            resId = R.drawable.pic_title_coupons;
//        } else if (title.equals("待激活")) {
//            resId = R.drawable.pic_title_coupon_details;
//        } else if (title.equals("停车缴费")) {
//            resId = R.drawable.pic_title_orders;
//        } else if (title.equals("缴费")) {
//            resId = R.drawable.pic_title_pay;
//        } else if (title.equals("我的关注")) {
//            resId = R.drawable.pic_title_fav;
//        } else if (title.equals("我的充值")) {
//            resId = R.drawable.pic_title_my_wallet;
//        } else if (title.equals("立即充值")) {
//            resId = R.drawable.pic_title_recharge;
//        } else {
//            resId = R.drawable.img_title_main;
//        }
//
//        if (mIvTitle != null) {
//            mIvTitle.setBackgroundResource(resId);
//        }

        super.onReceivedTitle(view, title);
    }

    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        super.onProgressChanged(view, newProgress);
    }

    /**
     * 覆盖默认的window.alert展示界面，避免title里显示网址地址
     */
    @Override
    public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
        builder.setTitle(R.string.app_name)
                .setMessage(message)
                // 不需要绑定按键事件
                .setPositiveButton("确定", null)
                //屏蔽keycode等于84(search)之类的按键
                .setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        return true;
                    }
                })
                // 禁止响应按back键的事件
                .setCancelable(false);
        AlertDialog dialog = builder.create();
        dialog.show();
        // 因为没有绑定事件，需要强行confirm,否则页面会变黑显示不了内容
        result.confirm();
        return true;
    }

    /**
     * 覆盖默认的window.confirm展示界面，避免title里显示网址地址
     */
    @Override
    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
        builder.setTitle(R.string.app_name)
                .setMessage(message)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm();
                    }
                })
                .setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.cancel();
                    }
                })
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        result.cancel();
                    }
                })
                // 屏蔽keycode等于84(search)之类的按键，避免按键后导致对话框消息而页面无法再弹出对话框的问题
                .setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode,
                                         KeyEvent event) {
                        return true;
                    }
                });
        //禁止响应按back键的事件
//        builder.setCancelable(false);
        AlertDialog dialog = builder.create();
        dialog.show();
        return true;
    }

    /**
     * 覆盖默认的window.prompt展示界面，避免title里显示网址地址
     */
    @Override
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result) {
        final EditText et = new EditText(view.getContext());
        et.setSingleLine();
        et.setText(defaultValue);

        final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
        builder.setTitle(R.string.app_name)
                .setMessage(message)
                .setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm(et.getText().toString());
                    }
                })
                .setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.cancel();
                    }
                })
                // 屏蔽keycode等于84(search)之类的按键，避免按键后导致对话框消息而页面无法再弹出对话框的问题
                .setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode,
                                         KeyEvent event) {
                        return true;
                    }
                });
        // 禁止响应按back键的事件
//        builder.setCancelable(false);
        AlertDialog dialog = builder.create();
        dialog.show();
        return true;
    }


    // For Android 3.0+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
//        if (mUploadMessage != null) return;
//        mUploadMessage = uploadMsg;
//        selectImage();
    }
    /*
    // For Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        openFileChooser( uploadMsg, "" );
    }
    */
    // For Android  > 4.1.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        openFileChooser(uploadMsg, acceptType);
    }
}
